package com.zhang.yygh.user.controller.user;


import com.atguigu.yygh.model.user.Patient;
import com.zhang.yygh.common.result.R;
import com.zhang.yygh.user.service.PatientService;
import com.zhang.yygh.user.util.AuthContextHolder;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 就诊人表 前端控制器
 * </p>
 *
 * @author zhang
 * @since 2022-03-14
 */
//就诊人管理接口
@RestController
@RequestMapping("/api/userinfo/patient")
public class PatientController {

    @Autowired
    private PatientService patientService;

    //就诊人添加
    @PostMapping("auth/save")
    public R addPatient(@RequestBody Patient patient, HttpServletRequest request){
        //根据请求报文返回用户id
        patient.setUserId(AuthContextHolder.getUserId(request));
        patientService.save(patient);
        return R.ok();
    }

    //就诊人删除
    @DeleteMapping("/delete/{pid}")
    public R deletePatientById(@PathVariable Long pid){
        patientService.removeById(pid);
        return R.ok();
    }

    //就诊人修改：①根据就诊人id查询就诊人信息
    @GetMapping("/info/{pid}")
    public R getPatientInfo(@PathVariable Long pid){
        Patient patient=patientService.getPatientInfo(pid);
        return R.ok().data("patient",patient);
    }

    //②真实修改
    @PutMapping("/update")
    public R updatePatient(@RequestBody Patient patient){
        //携带哪些字段就修改哪些（patient里已含有就诊人id）
        patientService.updateById(patient);
        return R.ok();
    }

    //查询：当前登录用户下面所有的就诊人信息
    @GetMapping("/list")
    public R getPatientList(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> patientList=patientService.getPatientList(userId);
        return R.ok().data("list",patientList);
    }

    //这个接口也是给service_order模块远程调用的
    @ApiOperation(value = "根据id获取就诊人信息")
    @GetMapping("inner/get/{id}")
    public Patient getPatientById(
            @ApiParam(name = "id", value = "就诊人id", required = true)
            @PathVariable("id") Long id) {
        return patientService.getById(id);
    }
}

